package co.edu.uniquindio.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.ImageIcon;

import co.edu.uniquindio.delegados.ParametrizadaDelegado;
import co.edu.uniquindio.logica.Motor;

public class VentanaMotor extends JFrame implements ActionListener,WindowListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
    public static VentanaMotor me;
    private JTextField campoReferencia;
    private JTextField campoPotencia;
    private JTextField campoCilindraje;
	private JButton btnAceptar;
	private ParametrizadaDelegado param= ParametrizadaDelegado.getParametrizadaDelegado();
	//private VentanaPrincipal vp=VentanaPrincipal.getVentanaPrincipal();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VentanaMotor frame = new VentanaMotor();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public VentanaMotor() {
		super();
		initGUI();
	}
	/**
	 * este metodo em permite crear instancia unica de la clase VentanaMotor 
	 * @return
	 */
	public static VentanaMotor getVentanaMotor()
	{
		if(me==null)
			me=new VentanaMotor();
		return me;
	}
	/**
	 * este metodo inicializa los componentes de la clase
	 */
    public void initGUI()
    {
    	
    	setTitle("Motor");
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		getContentPane().setBackground(Color.WHITE);
		
		getContentPane().setLayout(null);
		
		JLabel lblIngresePorFavor = new JLabel("Ingrese por favor los datos para ingresar un nuevo motor :");
		lblIngresePorFavor.setBounds(24, 12, 406, 25);
		getContentPane().add(lblIngresePorFavor);
		
		JLabel lblReferencia = new JLabel("Referencia :");
		lblReferencia.setBounds(24, 49, 101, 25);
		getContentPane().add(lblReferencia);
		
		JLabel lblPotencia = new JLabel("Potencia :");
		lblPotencia.setBounds(24, 86, 77, 25);
		getContentPane().add(lblPotencia);
		
		JLabel lblCilindraje = new JLabel("Cilindraje :");
		lblCilindraje.setBounds(24, 123, 77, 25);
		getContentPane().add(lblCilindraje);
		
		campoReferencia = new JTextField();
		campoReferencia.setBounds(106, 48, 122, 26);
		getContentPane().add(campoReferencia);
		campoReferencia.setColumns(10);
		
		campoPotencia = new JTextField();
		campoPotencia.setBounds(106, 86, 122, 26);
		getContentPane().add(campoPotencia);
		campoPotencia.setColumns(10);
		
		campoCilindraje = new JTextField();
		campoCilindraje.setBounds(106, 122, 122, 26);
		getContentPane().add(campoCilindraje);
		campoCilindraje.setColumns(10);
		
		btnAceptar = new JButton("Aceptar");
		btnAceptar.setBounds(106, 159, 108, 26);
		btnAceptar.addActionListener(this);
		getContentPane().add(btnAceptar);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(VentanaMotor.class.getResource("/co/edu/uniquindio/imagenes/motor.png")));
		label.setBounds(240, 34, 217, 221);
		getContentPane().add(label);
    }
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(arg0.getSource()==btnAceptar)
		{
			try {
				int ref=Integer.parseInt(campoReferencia.getText());
				int potencia=Integer.parseInt(campoPotencia.getText());
				int cilindraje=	Integer.parseInt(campoCilindraje.getText());
				Motor motor= new Motor(ref, potencia, cilindraje);
				try {
					param.agregarMotor(motor);
				    campoCilindraje.setText("");
				    campoReferencia.setText("");
				    campoPotencia.setText("");
				    JOptionPane.showMessageDialog(this, "el motor se ha agregado con exito");
				
				} catch (Exception e) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(this, e.getMessage());
				}
				
				
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(this, "verifique los datos");
				// TODO: handle exception
			}
			
		}
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		this.setVisible(false);
		//vp.setVisible(true);
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
}
